import { Common } from 'UtilsCommon';
import BaseIndex from "../../baseIndex";

export default class DataGridView extends BaseIndex {
  constructor(props) {
    super(props);

    this.init();
  }

  searchQuery(id, actionType, data) {
    const { queryInfo, pageSize, pageIndex, entity, isData, dataAction } = data;

    let expandMethods = entity.expandMethods;
    if (dataAction && dataAction.searchQuery) expandMethods = { searchQuery: dataAction.searchQuery };

    const entityName = entity.viewName || entity.name;

    let methodName = entity.isGroupByInfo ? entity.queryPageUrl || "/Select2" : "";

    if (expandMethods && expandMethods.searchQuery) methodName = '/' + expandMethods.searchQuery;

    let dataUrl = `${entityName}${methodName}?$query=true&$data=true&pagesize=${pageSize}&pageindex=${pageIndex}`;

    if (!isData) dataUrl = Common.addUrlParams(dataUrl, "$page", true);
    if (entity.isGroupByInfo) dataUrl = Common.addUrlParams(dataUrl, "$groupbyinfo", "true");

    data.QueryInfo = queryInfo;
    data.url = dataUrl;

    if (data.entitySearchQuery) {
      data.dataGridViewSearchQuery = actionType;
      data.entityName = entityName;
      data.dataAction = dataAction;
      this.invokeAction(id, data.entitySearchQuery, data);
    }
  }

  setsearchQuery(id, action, data) {
    if (!this.receives[id]) return false;
    return data;
  }

  excelExport(id, actionType, data) {
    const { queryInfo, entity, title, dataAction } = data;

    const entityName = entity.viewName || entity.name;

    const dataUrl = `?$query=true&Action=Excel&Title=${encodeURIComponent(title)}`;

    let url = entityName;

    let expandMethods = entity.expandMethods;
    if (dataAction && dataAction.excelExport) expandMethods = { excelExport: dataAction.excelExport };

    if (expandMethods && expandMethods.excelExport) url += "/" + expandMethods.excelExport + dataUrl;
    else url += dataUrl;

    if (data.entityExcelExport) {
      this.invokeAction(id, data.entityExcelExport, { url, dataAction, pathQuery: dataUrl, dataGridViewExcelExport: actionType, QueryInfo: queryInfo });
    }
  }

  setexcelExport(id, action, data) {
    if (!this.receives[id]) return false;
    return data;
  }
}
